package com.lwj.liwanjia.view.activity.personal.qian_bao;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.TxRecordAdapter;
import com.lwj.liwanjia.base.BaseActivity;

import com.lwj.liwanjia.bean.RecordBean;
import com.lwj.liwanjia.presenter.RecordPresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.impl.RecordView;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import butterknife.OnClick;

public class RecordActivity extends BaseActivity implements RecordView, SuperRecyclerView.LoadingListener {


    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.record_rlv)
    SuperRecyclerView recordRlv;
    @BindView(R.id.iv_record)
    ImageView ivRecord;

    private RecordPresenter presenter;
    private List<RecordBean.DataBean> list = new ArrayList<>();
    private TxRecordAdapter adapter;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("提现记录");
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recordRlv.setLayoutManager(manager);
        recordRlv.setRefreshEnabled(true);
        recordRlv.setLoadMoreEnabled(false);
        recordRlv.setLoadingListener(this);
        adapter = new TxRecordAdapter(this, list);
        recordRlv.setAdapter(adapter);
        presenter = new RecordPresenter(this);
    }

    @Override
    protected void initData() {
        presenter.getRecordData(SpfUtils.getSpfUtils(this).getToken());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_record;
    }

    @OnClick(R.id.tv_left)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onRefresh() {
        initData();
        recordRlv.completeRefresh();
    }

    @Override
    public void onLoadMore() {

    }


    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getRecordData(RecordBean recordBean) {
        if (recordBean.getState() == 0) {
            Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
        } else if (recordBean.getState() == 1) {
            list.clear();
            list.addAll(recordBean.getData());
            if (list.size() > 0) {
                recordRlv.setVisibility(View.VISIBLE);
                adapter.notifyDataSetChanged();
                ivRecord.setVisibility(View.GONE);
            } else {
                recordRlv.setVisibility(View.GONE);
                ivRecord.setVisibility(View.VISIBLE);
            }
        }
    }

}
